using Mono.Data.Sqlite;
using System.IO;
using System;
using System.Configuration.Assemblies;
using System.Linq;

namespace RssReader.Data.Properties
{
	using System.Reflection;
	
	public sealed partial class Settings : System.Collections.Generic.Dictionary<string, string>
	{

		static internal readonly Settings defaultInstance = new Settings ();

		static Settings ()
		{
			string schema = System.Reflection.Assembly.GetAssembly(typeof(Settings)).FullName.Split (',')[1].Split ('=')[1].Replace (".", "_");
			defaultInstance.Add ("FeedsConnectionString", "Data Source=" + Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "MobileRss_" + schema + ".db3"));
			CheckFile (defaultInstance["FeedsConnectionString"].Remove (0, 12));
		}

		public static Settings Default {
			get { return defaultInstance; }
		}

		public string FeedsConnectionString {
			get { return ((string)(this["FeedsConnectionString"])); }
		}

		static void CheckFile (string fileLoc)
		{
			if (!File.Exists (fileLoc)) {
				SqliteConnection.CreateFile (fileLoc);
				SqliteConnection conn = new SqliteConnection (Default.FeedsConnectionString);
				SqliteCommand cmd = new SqliteCommand (@"
									CREATE TABLE Sites (
										SiteID INTEGER PRIMARY KEY AUTOINCREMENT,
										SiteURL NTEXT,
										SiteTitle NTEXT,
										SiteImageURL
									);
									CREATE TABLE Feeds (
										FeedID INTEGER PRIMARY KEY AUTOINCREMENT, 
										SiteID INTEGER NOT NULL CONSTRAINT FK_SITES_FEEDS REFERENCES Sites(SiteID) ON DELETE CASCADE,
										FeedTitle NTEXT,
										FeedURL NTEXT, 
										DisplayOrder INTEGER
									);
									CREATE TABLE Articles (
										ArticleID INTEGER PRIMARY KEY AUTOINCREMENT,
										FeedID INTEGER NOT NULL CONSTRAINT FK_FEEDS_ARTICLES REFERENCES Feeds(FeedID) ON DELETE CASCADE, 
										Title NTEXT  NOT NULL,
										IsRead CHAR(1)  DEFAULT 'N',
										Link NTEXT   NOT NULL,
										Description NTEXT   NOT NULL,
										PubDate DATETIME  NOT NULL
									);
									INSERT INTO Sites (SiteURL,SiteTitle) VALUES('http://www.cnn.com/services/rss/','CNN');
									INSERT INTO Sites (SiteURL,SiteTitle) VALUES('http://www.historyorb.com/today/rss-feeds.php','Today in History');
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Top Stories','http://rss.cnn.com/rss/cnn_topstories.rss',1);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'World','http://rss.cnn.com/rss/cnn_world.rss',2);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'U.S.','http://rss.cnn.com/rss/cnn_us.rss',3);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Sports (SI.com)','http://rss.cnn.com/rss/si_topstories.rss',4);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Business (CNNMoney.com)','http://rss.cnn.com/rss/money_latest.rss',5);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Politics','http://rss.cnn.com/rss/cnn_allpolitics.rss',6);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Crime','http://rss.cnn.com/rss/cnn_crime.rss',7);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Technology','http://rss.cnn.com/rss/cnn_tech.rss',8);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Science & Space','http://rss.cnn.com/rss/cnn_space.rss',9);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Health','http://rss.cnn.com/rss/cnn_health.rss',10);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Entertainment','http://rss.cnn.com/rss/cnn_showbiz.rss',11);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Travel','http://rss.cnn.com/rss/cnn_travel.rss',12);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Living','http://rss.cnn.com/rss/cnn_living.rss',13);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Video','http://rss.cnn.com/rss/cnn_freevideo.rss',14);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'CNN Student News','http://rss.cnn.com/rss/cnn_studentnews.rss',15);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Most Popular','http://rss.cnn.com/rss/cnn_mostpopular.rss',16);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(1,'Most Recent','http://rss.cnn.com/rss/cnn_latest.rss',17);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(2,'Daily Today in History','http://feeds.feedburner.com/historyorb/todayinhistory',1);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(2,'Daily Historical Events','http://feeds.feedburner.com/historicalevents',2);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(2,'Daily Famous Birthdays','http://feeds.feedburner.com/historyorb/famousbirthdays',3);
									INSERT INTO Feeds (SiteID, FeedTitle, FeedURL, DisplayOrder) VALUES(2,'Daily Famous Deaths','http://feeds.feedburner.com/famousdeaths',4);", conn);
				
				conn.Open ();
				cmd.ExecuteNonQuery ();
				conn.Close ();
				cmd.Dispose ();
				conn.Dispose ();
			}
		}
		
	}
}
